package cn.nubia.admin.sysconfig.controller;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.sysconfig.bean.MenuResource;
import cn.nubia.admin.sysconfig.dto.MenuResourceDTO;
import cn.nubia.admin.sysconfig.query.MenuResourceParam;
import cn.nubia.admin.sysconfig.service.MenuResourceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Jason on 2015/6/29.
 */
@Controller
@RequestMapping(value = "/menures")
public class MenuResourceController {

    @Autowired
    MenuResourceService menuResourceService;

    /**
     * 查看资源列表.
     *
     * @param param 请求参数
     * @return 资源列表对象
     */
    @RequiresPermissions("sys:menures:menuresList")
    @RequestMapping(value = "menuresList", method = RequestMethod.GET)
    public ModelAndView getList(MenuResourceParam param) {
        ModelAndView modelAndView = new ModelAndView("/menures/menuresList");
        Paged<MenuResource> paged = menuResourceService.getMenuResourcePage(param.getPageNo(), param.getPageSize());
        modelAndView.addObject("resourcePage", paged);
        return modelAndView;
    }

    /**
     * 查看资源
     *
     * @param resourceId 资源id
     * @return 查看的资源列表
     */
    @RequestMapping(value = "view", method = RequestMethod.GET)
    public ModelAndView view(Integer resourceId) {
        ModelAndView modelAndView = new ModelAndView("/menures/menuresForm");
        MenuResourceDTO menuResource = menuResourceService.getResource(resourceId);
        if (menuResource == null) {
            menuResource = new MenuResourceDTO();
        }
        modelAndView.addObject("resourceList",menuResourceService.getMenuResourceRootList());
        modelAndView.addObject("resource", menuResource);
        return modelAndView;
    }


    /**
     * 修改或者保存资源
     *
     * @param menuResource 资源对象
     * @return 成功标志
     */
    @ResponseBody
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public Response save(MenuResource menuResource) {
        boolean flag = menuResourceService.save(menuResource);
        if (flag) {
            return Response.getSuccessResponse("保存成功");
        }else{
            return Response.getSuccessResponse("保存失败");
        }
    }


}
